package com.init.engineering.controller;

import com.init.engineering.pages.PageResult;
import com.init.engineering.pages.QueryPageBean;
import com.init.engineering.pages.Result;
import com.init.engineering.pojo.Stage;
import com.init.engineering.service.StageService;
import com.init.engineering.utils.UUIDUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/stage")
public class StageController {
    @Autowired
    private StageService stageService;

    /**
     * 分页查询所有工程阶段
     * @param queryPageBean
     * @return
     */
    @PostMapping("/all")
    @ApiOperation(value = "分页条件查询所有的工程阶段",notes = "查询接口",httpMethod = "post")
    public PageResult findStagesByPages(@RequestBody QueryPageBean queryPageBean){
        PageResult stagesByPage = stageService.findStagesByPage(queryPageBean);
        return stagesByPage;
    }

    /**
     * 新增工程阶段
     * @RequestBody 来接收前端传递给后端的json字符串中的数据,将该json数据对象转为java对象
     * @param stage
     * @return
     */
    @PostMapping("/add")
    @ApiOperation(value = "新增工程阶段",notes = "添加接口",httpMethod = "post")
    public Result addStage(@RequestBody Stage stage){

        System.out.println(stage.toString());
        //随机ID
        stage.setSid(UUIDUtil.GetID());
        //获取系统更新时间
        stage.setUpdatetime(new Date());
        int row = stageService.saveStage(stage);
        Result result = new Result();
        if(row > 0){
            result.setMessage("添加成功");
            result.setFlag(true);
        }else{
            result.setFlag(false);
            result.setMessage("添加失败，请重试");
        }
        return result;
    }

    /**
     * 删除工程阶段
     * @param sid
     * @return
     */
    @GetMapping("/delete/{sid}")
    @ApiOperation(value = "删除工程阶段",notes = "删除接口",httpMethod = "get")
    public Result deleteStage(@PathVariable("sid") String sid){
        int row = stageService.deleteStage(sid);
        Result result = new Result();
        if(row > 0){
            result.setMessage("删除成功");
            result.setFlag(true);
        }else{
            result.setFlag(false);
            result.setMessage("删除失败，请重试");
        }
        return result;
    }

    /**
     * 通过工程阶段id查找
     * @param sid
     * @return
     */
    @GetMapping("/findStageBySid")
    public Result findStageBySid(String sid){
        Stage stage = stageService.findStageBySid(sid);
        Result result = new Result();
        if (stage != null){
            result.setFlag(true);
            result.setMessage("回显成功");
            result.setData(stage);
        }else{
            result.setFlag(false);
            result.setMessage("数据回显失败");
        }
        return result;
    }

    /**
     * 进行工程阶段的更新
     * @param stage
     * @return
     */
    @PostMapping("/update")
    @ApiOperation(value = "更新工程阶段",notes = "更新接口",httpMethod = "post")
    public Result updateStage(@RequestBody Stage stage){
        stage.setUpdatetime(new Date());
        int row = stageService.updateStage(stage);
        Result result = new Result();
        if(row > 0){
            result.setMessage("更新成功");
            result.setFlag(true);
        }else{
            result.setFlag(false);
            result.setMessage("更新失败，请重试");
        }
        return result;
    }

    /**
     * 查询所有工程阶段
     * @return
     */
    @RequestMapping("/findProjects")
    public Result findProjects(){
        List<Stage> stages = stageService.findStages();
        Result result = new Result();
        if(stages != null){
            result.setData(stages);
            result.setFlag(true);
        }else{
            result.setFlag(false);
        }
        return result;
    }
}
